package cn.itqiqi.student_system_java.service.impl;

import cn.hutool.json.JSONUtil;
import cn.itqiqi.student_system_java.mapper.ScoreMapper;
import cn.itqiqi.student_system_java.pojo.*;
import cn.itqiqi.student_system_java.service.ScoreService;
import cn.itqiqi.student_system_java.utils.ResultUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author kevin_琪琪
 * @version 1.0
 * @description
 * @date 2022-06-20 17:43
 */

@Service
public class ScoreServiceImpl implements ScoreService {

    @Autowired
    private ScoreMapper scoreMapper;
    @Autowired
    private ResultUtils resultUtils;

    @Override
    public String getScoreData(Long pageNum, Long pageSize) {
        Page<Score> scorePage = new Page<>(pageNum, pageSize);
        List<Score> scoreAll = scoreMapper.getScoreAll(scorePage);
        ScoreVo scoreVo = new ScoreVo();
        scoreVo.setTotal(scorePage.getTotal());
        scoreVo.setScoreList(scoreAll);
        return resultUtils.success(scoreVo);
    }

    @Override
    public String getCourseAll() {
        List<Course> courseAll = scoreMapper.getCourseAll();
        return resultUtils.success(courseAll);
    }

    @Override
    public String getStuAll() {
        ArrayList<ClassNumVo> classNumVos = new ArrayList<>();
        int classNum = scoreMapper.getClassNum();
        for (int i = 1; i <= classNum; i++) {
            ClassNumVo classNumVo = new ClassNumVo();
            ClassNumVo classInfo = scoreMapper.getClassInfo(i);
            List<ClassNumVo> stuInfo = scoreMapper.getStuInfo(i);
            classNumVo.setValue(classInfo.getValue());
            classNumVo.setLabel(classInfo.getLabel());
            classNumVo.setChildren(stuInfo);
            classNumVos.add(classNumVo);
        }
        return resultUtils.success(classNumVos);
    }

    @Override
    public String getCourseByName(Long pageNum, Long pageSize, String queryValue) {
        Page<Score> scorePage = new Page<>(pageNum, pageSize);
        List<Score> courseByName = scoreMapper.getCourseByName(scorePage, queryValue);
        ScoreVo scoreVo = new ScoreVo();
        scoreVo.setTotal(scorePage.getTotal());
        scoreVo.setScoreList(courseByName);
        return resultUtils.success(scoreVo);
    }

    @Override
    public String getCourseByCourse(Long pageNum, Long pageSize, String queryValue) {
        Page<Score> scorePage = new Page<>(pageNum, pageSize);
        List<Score> courseByName = scoreMapper.getCourseByCourse(scorePage, queryValue);
        ScoreVo scoreVo = new ScoreVo();
        scoreVo.setTotal(scorePage.getTotal());
        scoreVo.setScoreList(courseByName);
        return resultUtils.success(scoreVo);
    }

    /**
     * 成绩添加
     * @param scoreAddVo 学生信息和课程信息
     * @return json
     */
    @Override
    public String scoreAdd(String scoreAddVo) {
        ScoreAddVo scoreBean = JSONUtil.toBean(scoreAddVo, ScoreAddVo.class);
        if (scoreBean.getSid() != null && scoreBean.getCid() != null && scoreBean.getScore() != null && scoreBean.getSid().size() == 2) {
            int i = scoreMapper.scoreNum(Integer.parseInt(scoreBean.getSid().get(1)), Integer.parseInt(scoreBean.getCid()));
            if (i >= 1) {
                return resultUtils.fail(406, "该学生成绩已存在！");
            }
            scoreMapper.scoreAdd(Integer.parseInt(scoreBean.getSid().get(1)), Integer.parseInt(scoreBean.getCid()), scoreBean.getScore());
            return resultUtils.success("添加成功!");
        } else {
            return resultUtils.fail(400, "参数不合法！");
        }
    }

    /**
     * 修改学生成绩信息
     * @param score 成绩信息
     * @return json
     */
    @Override
    public String updateScore(String score) {
        Score scoreBean = JSONUtil.toBean(score, Score.class);
        if (scoreBean.getId() != null && scoreBean.getScore() != null) {
            try {
                scoreMapper.updateScore(Integer.parseInt(scoreBean.getId().toString()), scoreBean.getScore());
                return resultUtils.success("修改成功！");
            } catch (Exception e) {
                return resultUtils.fail(406, "修改失败！");
            }
        } else {
            return resultUtils.fail(400, "参数不合法！");
        }
    }

    @Override
    public String deleteScore(String score) {
        Score scoreBean = JSONUtil.toBean(score, Score.class);
        if (scoreBean.getId() != null) {
            try {
                scoreMapper.deleteScore(Integer.parseInt(scoreBean.getId().toString()));
                return resultUtils.success("删除成功！");
            } catch (Exception e) {
                return resultUtils.fail(406, "删除失败！");
            }
        } else {
            return resultUtils.fail(400, "参数不合法！");
        }
    }


}
